package utils;

import static com.qb.devstore.ViewActivity.vActivity;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import com.qb.devstore.R;
import com.xuexiang.xui.widget.popupwindow.bar.CookieBar;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class MyUtil {
    //判断字符串是否为空
    public static boolean isNull(Object s) {
        if (s == null) {
            return true;
        } else if (s.equals("null")) {
            return true;
        } else if (s.equals("")) {
            return true;
        } else if (s.toString().equals("{}")) {
            return true;
        } else if (s.toString().equals("[]")) {
            return true;
        }else if (s.toString().length() <= 0) {
            return true;
        } else {
            return false;
        }
    }
    // toast与log
    public static void showToast(Context ctx,String s) {
        Toast.makeText(ctx, s, Toast.LENGTH_LONG).show();
    }
    public static void showLog(String tag, Object obj) {
//        if (!tag.equals("")) System.out.println("******"+tag+"******");
//        if (!isNull(obj)) System.out.println(obj);
        // Log.v(tag.equals("")?"":tag, String.valueOf(obj));
    }
    // cookieBar
    public static void showCookieBar(Context ctx,String title,String msg,String flag) {
        String actionStr;
        if (flag.equals("login")) {
            actionStr = "重新连接";
        } else {
            actionStr = "关闭";
        }
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                CookieBar.builder((Activity) ctx)
                        .setTitle(title)
                        .setMessage(msg)
                        .setDuration(-1)
                        //.setLayoutGravity(1)
                        .setBackgroundColor(R.color.red_start)
                        .setActionColor(R.color.white)
                        .setTitleColor(R.color.red_end)
                        .setAction(actionStr, view1 -> {
                            if (flag.equals("login")) {
                                //System.exit(0);
                                vActivity.login();
                            }
                        })
                        .show();
            }
        });
    }
    // 日期格式化
    public static String formatDateTime(Date date) {
        SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss", Locale.getDefault());
        String myDate = format.format(date);
        return myDate;
    }
    // long 转日期字符串
    public static String transferLongToDate(String dateFormat, Long millSec){
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date date= new Date(millSec);
        return sdf.format(date);
    }
    //文件夹是否存在 不存在则创建
    public static boolean isFolderExists(String strFolder) {
        File file = new File(strFolder);
        if (file.exists()){
            return true;
        } else{
            if (file.mkdir()) {
                return true;
            } else {
                if (file.exists()) {
                    return true;
                }
                else return false;
            }
        }
    }
    // String[] 添加元素
    public static String[] strArrInsert(String[] arr, String str) {
        int size = arr.length;
        String[] tmp = new String[size + 1];
        System.arraycopy(arr, 0, tmp, 0, size);
        tmp[size] = str;
        return tmp;
    }
    // 合并两个JSONArray
    public static JSONArray joinJSONArray(JSONArray mData, JSONArray array) {
        if (array.length() <=0 ) return mData;
        StringBuffer buffer = new StringBuffer();
        try {
            int len = mData.length();
            for (int i = 0; i < len; i++) {
                JSONObject obj1 = (JSONObject) mData.get(i);
                if (i == len - 1)
                    buffer.append(obj1.toString());
                else
                    buffer.append(obj1.toString()).append(",");
            }
            len = array.length();
            if (len > 0)
                buffer.append(",");
            for (int i = 0; i < len; i++) {
                JSONObject obj1 = (JSONObject) array.get(i);
                if (i == len - 1)
                    buffer.append(obj1.toString());
                else
                    buffer.append(obj1.toString()).append(",");
            }
            buffer.insert(0, "[").append("]");
            return new JSONArray(buffer.toString());
        } catch (Exception e) {
        }
        return null;
    }
}
